using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using ApiProject.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//商品
namespace ApiProject.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class WebController : ControllerBase
    {
        private IRepository<EmporiumInfo> _webRepository;

        private readonly IWebHostEnvironment _hostingEnvironment;

        public WebController(
            IRepository<EmporiumInfo> webRepository,
            IWebHostEnvironment hostingEnvironment
        )
        {
            _webRepository = webRepository;
            _hostingEnvironment = hostingEnvironment;
        }


        //添加网站设置
        //Post /web/id
        [HttpPost("{id}")]
        public dynamic AddWebInfo(int id)
        {

            var files = HttpContext.Request.Form.Files;
            var webAddress = HttpContext.Request.Form["webAddress"].ToString();
            var emporiumName = HttpContext.Request.Form["emporiumName"].ToString();
            var icp = HttpContext.Request.Form["icp"].ToString();
            var policeRecord = HttpContext.Request.Form["policeRecord"].ToString();
            var copyright = HttpContext.Request.Form["copyright"].ToString();
            var telephone = HttpContext.Request.Form["telephone"].ToString();
            var dzyx = HttpContext.Request.Form["dzyx"].ToString();

            string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
            var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

            if (id == 0)
            {
                var newWebInfo = new EmporiumInfo
                {
                    LogoPath = showfilePath,
                    WebAddress = webAddress,
                    EmporiumName = emporiumName,
                    ICP = icp,
                    PoliceRecord = policeRecord,
                    Copyrigh = copyright,
                    Telephone = telephone,
                    Dzyx = dzyx
                };

                _webRepository.Insert(newWebInfo);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = newWebInfo,
                    Msg = "网站信息设置成功"
                });
            }
            else
            {

                var isWebInfo = _webRepository.GetById(id);

                isWebInfo.IsDelete = true;
                isWebInfo.UpdateTime = DateTime.Now;

                _webRepository.Update(isWebInfo);

                var newWebInfo = new EmporiumInfo
                {
                    LogoPath = showfilePath,
                    WebAddress = webAddress,
                    EmporiumName = emporiumName,
                    ICP = icp,
                    PoliceRecord = policeRecord,
                    Copyrigh = copyright,
                    Telephone = telephone,
                    Dzyx = dzyx
                };

                _webRepository.Insert(newWebInfo);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = newWebInfo,
                    Msg = "网站信息设置成功"
                });

            }


        }



        // 查找最新的网站信息
        // Get /web
        [HttpGet]
        public dynamic GetWebInfo()
        {
            var webInfo = _webRepository.Table.Where(x => x.IsDelete == false).FirstOrDefault();


            if (webInfo == null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = "",
                    Msg = ""
                });
            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = webInfo,
                    Msg = ""
                });
            }

        }





    }
}
